/*
 * @Descripttion: 
 * @version: V1.0.0
 * @Author: LeonHe
 * @Date: 2021-05-25 23:07:48
 * @LastEditTime: 2021-05-29 18:00:03
 */

#include "mb.h"
#include "mbport.h"
#include "ParamMng.h"


/* ----------------------- Defines ------------------------------------------*/
#define REG_INPUT_START 1000+1
#define REG_INPUT_NREGS 1
#define REG_HOLD_START 2000+1
#define REG_HOLD_NREGS (sizeof(ParamListStDef)/sizeof(USHORT) + 10)

/* ----------------------- Static variables ---------------------------------*/
USHORT   usRegInputStart = REG_INPUT_START;
USHORT   usRegInputBuf[REG_INPUT_NREGS];
USHORT   usRegHoldStart = REG_HOLD_START;
USHORT   usRegHoldBuf[REG_HOLD_NREGS];


eMBErrorCode
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
{
    eMBErrorCode    eStatus = MB_ENOERR;
    int             iRegIndex;

    if( ( usAddress >= REG_INPUT_START )
        && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
    {
        iRegIndex = ( int )( usAddress - usRegInputStart );
        while( usNRegs > 0 )
        {
            *pucRegBuffer++ =
                ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
            *pucRegBuffer++ =
                ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
            iRegIndex++;
            usNRegs--;
        }
    }
    else
    {
        eStatus = MB_ENOREG;
    }

    return eStatus;
}

eMBErrorCode
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
                 eMBRegisterMode eMode )
{
    eMBErrorCode    eStatus = MB_ENOERR;
    int             iRegIndex;

    switch(eMode)
    {
        case MB_REG_READ:
            if( ( usAddress >= REG_HOLD_START )
                && ( usAddress + usNRegs <= REG_HOLD_START + REG_HOLD_NREGS ) )
            {
                iRegIndex = ( int )( usAddress - usRegHoldStart );
                while( usNRegs > 0 )
                {
                    *pucRegBuffer++ =
                        ( unsigned char )( usRegHoldBuf[iRegIndex] >> 8 );
                    *pucRegBuffer++ =
                        ( unsigned char )( usRegHoldBuf[iRegIndex] & 0xFF );
                    iRegIndex++;
                    usNRegs--;
                }
            }
            else
            {
                eStatus = MB_ENOREG;
            }
            break;
        case MB_REG_WRITE:
            if( ( usAddress >= REG_HOLD_START )
                && ( usAddress + usNRegs <= REG_HOLD_START + REG_HOLD_NREGS ) )
            {
                iRegIndex = ( int )( usAddress - usRegHoldStart );
                while( usNRegs > 0 )
                {
                    usRegHoldBuf[iRegIndex] = 0;
                    usRegHoldBuf[iRegIndex] |= (*pucRegBuffer++)<<8;
                    usRegHoldBuf[iRegIndex] |= (*pucRegBuffer++)&0x00FF;
                    iRegIndex++;
                    usNRegs--;
                }
            }
            else
            {
                eStatus = MB_ENOREG;
            }
            break;
        default:
            return MB_EINVAL;
    }
    return MB_ENOERR;
}


eMBErrorCode
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
               eMBRegisterMode eMode )
{
    return MB_ENOREG;
}

eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
    return MB_ENOREG;
}
